using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using LocalBoard.Controller;
using LocalBoard.Domain;

namespace LocalBoard.Web
{
	/// <summary>
	/// Summary description for Register.
	/// </summary>
	public partial class Register : System.Web.UI.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(Request.IsAuthenticated)
			{
				userId.Text = ((ClubPrincipal)Context.User).Identity.Name;
				passwordRow.Visible = false;
				confirmPwdRow.Visible = false;
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void submitBtn_Click(object sender, System.EventArgs e)
		{
			if(!Request.IsAuthenticated && password.Text != confirmPassword.Text)
			{
				registrationError.Text = "Password fields have to be matched.";
				registrationError.Visible = true;
				return;
			}

			string photoPath = "";
			if(photoUpload.Value != null && photoUpload.Value.Length > 0
				&& photoUpload.PostedFile != null)
			{
				photoPath = new PhotoUploadController().WriteUploadedPhotoFile(photoUpload.PostedFile);
			}
			CreateUser(userId.Text, name.Text, password.Text, email.Text, photoPath, biography.Text);
			Security.RedirectToHome();
		}

		#region Helper
		private void CreateUser(string userId, string name, string password, string email, string photoPath, string biography)
		{
			AdminController adminController = new AdminController(Global.AccessorFactory);
			adminController.CreateUser(new Profile(userId, name, password, email, photoPath, biography));
		}
		#endregion
	}
}
